<?php
declare (strict_types=1);

// +----------------------------------------------------------------------
// | SCApi
// +----------------------------------------------------------------------
// | 版权所有 2021~2022 贾二小 [ http://www.ipsoul.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.ipsoul.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( http://doc.ipsoul.com/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/jiaerxiao/scapi
// | github 代码仓库：https://github.com/jiaerxiao/scapi
// +----------------------------------------------------------------------

namespace app\common\controller;

use app\BaseController;
use app\common\service\ToolService;
use app\system\facade\ValidateFacade;

/**
 * 基础控制器
 * @package app\api\controller3
 */
class SystemBaseController extends BaseController
{

    /**
     * 当前Token
     * @var
     */
    protected $token;

    /**
     * 当前用户
     * @var
     */
    protected $userData;

    protected $logName = '';

    public function initialize()
    {
        // 定义应用名
        defined('APP_NAME') or define('APP_NAME', App('http')->getName());
        // 定义控制器名
        defined('CONTROLLER_NAME') or define('CONTROLLER_NAME', $this->request->controller());
        // 定义操作方法名
        defined('ACTION_NAME') or define('ACTION_NAME', $this->request->action());
        // 当前Token
        $this->token = $this->request->token;
        if (!empty($this->token)) {
            // 当前用户
            $this->userData = cache($this->token);
        }
        parent::initialize();
    }

    /**
     * 操作成功
     * @param array $data 返回数据
     * @param string $msg 消息内容
     * @param int $code 返回代码
     * @author 贾二小
     * @since 2021/10/30
     */
    protected function success(array $data = [], string $msg = "操作成功", int $code = 200)
    {
        ToolService::success($data, $msg, $code, $this->logName);
    }

    /**
     * 操作失败
     * @author 贾二小
     * @since 2021/10/30
     */
    protected function error(string $msg, array $data = [], int $code = -1)
    {
        ToolService::error($msg, $data, $code, $this->logName);
    }

    /**
     * 表格数据
     * @param array $data 列表数据
     * @param string $msg 消息内容
     * @param int $code 返回代码
     * @author 贾二小
     * @since 2021/10/30
     */
    protected function table(array $data, string $msg = "操作成功", int $code = 200)
    {
        $data['page'] = intval(input('page'));
        $data['pageSize'] = intval(input('pageSize'));
        ToolService::success($data, $msg, $code, $this->logName);
    }

    /**
     * 请求参数验证
     * @author 贾二小
     * @since 2021/12/8
     */
    protected function validateData(array $data, bool $batch = false)
    {
        //获取验证规则
        $validateData = ValidateFacade::getData(APP_NAME, CONTROLLER_NAME, ACTION_NAME);
        if (!empty($validateData)) {
            //验证请求数据
            try {
                parent::validate($data, $validateData[0], $validateData[1], $batch);
            } catch (Exception $e) {
                ToolService::error($e->getMessage(), $data, -1, $this->logName);
            }
        }
    }

}